// import { Message, MessageBox } from 'element-ui'
import cookies from '@/utils/util.cookies.js';
import router from '@/router';
import {
	service,
	request
} from '../../../../api/service.js';

export default {
	namespaced: true,
	actions: {
		/**
		 * @description 登录
		 * @param {Object} context
		 * @param {Object} payload username {String} 用户账号
		 * @param {Object} payload password {String} 密码
		 * @param {Object} payload route {Object} 登录成功后定向的路由对象 任何 vue-router 支持的格式
		 */
		async setUsesrCookie({
			dispatch
		}, {
			response = {}
		} = {}) {

			cookies.remove('token');
			cookies.remove('uuid');
			cookies.remove('name');
			cookies.remove('accountBookListToWeb');
			cookies.remove('accountingbookname');

			const res = {
				uuid: response.data.userid,
				token: response.data.tokenstr,
				name: response.data.name,
				accountBookListToWeb: response.data.accountBookListToWeb,
				accountingbookname: response.data.accountingbookname
			};

			cookies.set('uuid', response.data.id);
			cookies.set('token', res.token);
			cookies.set('name', res.name);
			cookies.set('accountBookListToWeb', res.accountBookListToWeb);
			//登录时选择的账套
			cookies.set('accountingbookname', res.accountingbookname);
			return res;
		},
 
		//切换账套
		async changeAccountBook({
			dispatch
		}, {
			accountingbookcode = ''
		} = {}) {
			const response = await request({
				url: `Home/ChangeAccountBook?accountCode=${accountingbookcode}`,
				method: 'get'
			});
			if (response.status != 1) {
				return {
					flag: false,
					info: response.info
				};
			}


			var res = await dispatch('setUsesrCookie', {
				response: response
			});

			// 设置 vuex 用户信息
			await dispatch('admin/user/set', res, {
				root: true
			});
			// await dispatch('load');
			return {
				flag: true,
				info: response.info
			};


		},
		async login({
			dispatch
		}, {
			username = '',
			password = '',
			accountingbookcode = ''
		} = {}) {
			// const res = await api.SYS_USER_LOGIN({ username, password })

			const response = await service({
				url: 'Account/Login',
				method: 'post',
				data: {
					name: username,
					Password: password,
					accountingbookcode: accountingbookcode
				}
			});

			if (response.status != 1) {
				return {
					flag: false,
					info: response.info
				};
			}
			var res = await dispatch('setUsesrCookie', {
				response: response
			});

			// 设置 vuex 用户信息
			await dispatch('admin/user/set', res, {
				root: true
			});
			// 用户登录后从持久化数据加载一系列的设置
			await dispatch('load');
			return {
				flag: true,
				info: response.info
			};
		},
		/**
		 * @description 注销用户并返回登录页面
		 * @param {Object} context
		 * @param {Object} payload confirm {Boolean} 是否需要确认
		 */
		logout({
			commit,
			dispatch
		}, {
			confirm = false
		} = {}) {
			/**
			 * @description 注销
			 */
			async function logout() {


				// 删除cookie
				cookies.remove('token');
				cookies.remove('uuid');
				cookies.remove('name');
				cookies.remove('accountBookListToWeb');
				cookies.remove('accountingbookname');
				// 清空 vuex 用户信息
				await dispatch('admin/user/set', {}, {
					root: true
				});
				// 跳转路由
				router.push({
					name: 'login'
				});
			}
			// 判断是否需要确认
			if (confirm) {
				logout();

				// commit('admin/gray/set', true, { root: true })
				// MessageBox.confirm('确定要注销当前用户吗', '注销用户', { type: 'warning' })
				//   .then(() => {
				//     commit('admin/gray/set', false, { root: true })
				//     logout()
				//   })
				//   .catch(() => {
				//     commit('admin/gray/set', false, { root: true })
				//     Message({ message: '取消注销操作' })
				//   })
			} else {
				logout();
			}
		},
		/**
		 * @description 用户登录后从持久化数据加载一系列的设置
		 * @param {Object} context
		 */
		async load({
			dispatch
		}) {
			// 加载用户名
			await dispatch('admin/user/load', null, {
				root: true
			});
			// 是否为深色
			await dispatch('admin/theme/load', null, {
				root: true
			});
			// 持久化数据加载上次退出时的多页列表
			await dispatch('admin/page/openedLoad', null, {
				root: true
			});
			// 持久化数据加载侧边栏配置
			await dispatch('admin/menu/asideLoad', null, {
				root: true
			});
		}
	}
};
